<?php
namespace App\Service\OrderService\OrderNotifyService;

use Hyperf\Task\Annotation\Task;
use App\Service\Code\Normal;
use App\Model\CommodityModel\SupplierCompany;
use App\Service\VendorClient\ShopSanXiDaTong\ShopSanXiDaTongApiClient;
use Hyperf\Utils\WaitGroup;

/**
 * 三西大同供应商接口统一回调入口
 * @author chenshubo
 */

class ShopSanXiDaTongNotifyService extends BaseNotifyService
{
    protected $user_type    = 'shopsanxidatong';
    protected $message_type = 'OrderExpress';
    
    /**
     * @Task
     * 接收通知物流
     * @param array $param
     */
    public function QueryOrderClearance( array $param = [] ) 
    {
        if( empty($param['orderSns']) ) {
            return Normal::returnCode(Normal::FAIL,"orderSns参数不能为空");
        }
        
        $orderSns = array_unique($param['orderSns']);
        $orderSns = array_filter($orderSns);
        if( empty($orderSns) ) {
            return Normal::returnCode(Normal::FAIL,"当前订单集合数据orderSns格式错误");
        }
        
        
        $tableName = SupplierCompany::instance()->getTable();
        $apiBodyInfo = SupplierCompany::instance()
        ->from($tableName." as sp")
        ->select([
            'wh.JsonBody',
        ])
        ->join("warehouseinfo_s2b as wh","wh.SupplierID","=","sp.ID")
        ->where('sp.SupplierCode',$this->user_type)
        ->first();
        if( empty($apiBodyInfo) ) {
            return Normal::returnCode(Normal::FAIL,"云仓当前仓库参数不能为空");
        }
        
        $apiBody = @json_decode($apiBodyInfo->JsonBody,true);
        $result = [];
        $count = count($orderSns);
        $wg = new WaitGroup();
        $wg->add($count);
        foreach ($orderSns as $order_sn ) {
            co(function () use ($wg,$order_sn,$apiBody,& $result) {
                //调用三西大同供应商查询清关接口状态
                $res = (new ShopSanXiDaTongApiClient($apiBody))->queryOrderClearanceByOrderSn($order_sn);
                
                if( $res['code'] != Normal::SUC ) {
                    $returnInfo = [
                        "OrderNo"   => $order_sn,
                        "Code"      => '0',
                        "Message"   => "大同关务接口调用失败"
                    ];
                } else {
                    $returnInfo = $res['data'];
                }
                $result[] = $returnInfo;
                $wg->done();
            });
        }
        $wg->wait();
        return Normal::returnCode(Normal::SUC,"获取成功",$result);
    }
    
    /**
     * @Task
     * 接收通知物流
     * @param array $param
     */
    public function OrderExpress( $param = [] )
    {
        if( empty($param) ) {
            return $this->getErrorResponseCode();
        }
        
        $response = $this->getResponseCode($param);
        $this->response_to_req = $response;
        $this->message = $param;
        $result = $this->addNotifyCallBackLog();
        if( $result['code'] != Normal::SUC ) {
            return $this->getErrorResponseCode($param);
        }
        return $response;
    }
    
    /**
     * 获取接收响应标准
     * 参照当前上游通知业务标准来
     * 错误
     */
    public function getErrorResponseCode( array $param = [] )
    {
        if( empty($param['ExpressFeedBackReqs']) ) {
            return [
                'Code'      => '9999',
                'Message'   => '数据ExpressFeedBackReqs不能为空',
                'OrderNos'  => [],
            ];
        }
        
        return [
            'Code'      => '9999',
            'Message'   => '写入失败',
            'OrderNos'  => [],
        ];
    }
    
    /**
     * 获取接收响应标准
     * 参照当前上游通知业务标准来
     */
    public function getResponseCode( array $param = [] )
    {
        if( empty($param['ExpressFeedBackReqs']) ) {
            return [
                'Code'      => '9999',
                'Message'   => '数据ExpressFeedBackReqs不能为空',
                'OrderNos'  => [],
            ];
        }
        
        $orderNos = [];
        foreach ($param['ExpressFeedBackReqs'] as $item)
        {
            $orderNos[] = [
                'OrderNo' => $item['OrderNo'],
                'Code'    => '0000',
                'Message' => '接收成功'
            ];
        }
        
        return [
            'Code'      => '0000',
            'Message'   => '成功',
            'OrderNos'  => $orderNos,
        ];
    }
}